package br.folhapgto.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import br.folhapgto.dao.banco.Conexao;
import br.folhapgto.dao.idao.IVendaDAO;
import br.folhapgto.model.Venda;
import java.util.ArrayList;

public class VendaDAO implements IVendaDAO{

	private static VendaDAO instanciaUnica = new VendaDAO();

    public static VendaDAO getInstanciaUnica()
    {
        return instanciaUnica;
    }

    // Construtor
    public VendaDAO()
    {

    }

    // M�todos CRUD
    public Venda carregaDados(ResultSet rs) throws SQLException
    {
    	Venda venda = new Venda();

    	venda.setIdVendas(rs.getInt("idVendas"));
    	venda.setIdEmpregado(rs.getInt("idEmpregado"));
    	venda.setData(rs.getDate("data"));
        venda.setDescricao(rs.getString("descricao"));
        venda.setValor(rs.getFloat("valor"));

        return venda;
    }

    public Integer gravar(Venda venda)
    {

        int retornoBanco = 0;
        Connection conn = Conexao.getInstanciaUnica().criaConexao();

        if (conn != null) {
            PreparedStatement pstmt = null;
            try {
                pstmt = conn.prepareStatement(
                        "INSERT INTO TB_VENDAS (idEmpregado, " +
                        "data, " +
                        "descricao," +
                        "valor)" +
                        " VALUES (?, ?, ?, ?)");
                pstmt.setInt(1, venda.getIdEmpregado());
                pstmt.setDate(2, (venda.getData()));
                pstmt.setString(3, venda.getDescricao());
                pstmt.setFloat(4, venda.getValor());

                retornoBanco = pstmt.executeUpdate();
            } catch (SQLException e) {
                System.out.println("Inclusao Falhou!!!\n" + e.getMessage());
            } finally {
                try {
                    if (pstmt != null) {
                        pstmt.close();
                    }
                    if (conn != null) {
                        conn.close();
                    }
                } catch (SQLException e) {
                    System.out.println(e.getMessage());
                }
            }
        }

        return retornoBanco;
    }

    public List<Venda> lerTodos()
    {
    	Statement stmt = null;
        List<Venda> taxasServico = new ArrayList<Venda>();

        Connection conn = Conexao.getInstanciaUnica().criaConexao();

        if (conn != null) {
            try {
                stmt = conn.createStatement();
                ResultSet rs = stmt.executeQuery(
                        "SELECT * FROM TB_VENDAS");
                rs.last();
                rs.beforeFirst();
                while (rs.next()) {
                	taxasServico.add(carregaDados(rs));
                }
                conn.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            } finally {
                try {
                    if (stmt != null) {
                        stmt.close();
                    }
                    if (conn != null) {
                        conn.close();
                    }
                } catch (SQLException e) {
                    System.out.println(e.getMessage());
                }
            }
        }
        return taxasServico;
    }

	public Venda ler(Integer idVendas)
	{
		Statement stmt = null;
		Venda venda = null;
        Connection conn = Conexao.getInstanciaUnica().criaConexao();

        if (conn != null) {
            try {
                stmt = conn.createStatement();
                ResultSet rs = stmt.executeQuery(
                        "SELECT * FROM TB_TAXA_SERVICO WHERE idVendas = " +
                        idVendas);
                if (rs.next()) {
                	venda = carregaDados(rs);
                } else {
                	venda = null;
                }
                conn.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            } finally {
                try {
                    if (stmt != null) {
                        stmt.close();
                    }
                    if (conn != null) {
                        conn.close();
                    }
                } catch (SQLException e) {
                    System.out.println(e.getMessage());
                }
            }
        }
        return venda;
	}

	public Integer remover(Integer idVendas)
	{
		int n = 0;
        Statement stmt = null;
        Connection conn = Conexao.getInstanciaUnica().criaConexao();

        if (conn != null) {
            try {
                stmt = conn.createStatement();
                n = stmt.executeUpdate(
                        "DELETE FROM TB_TAXA_SERVICO WHERE idVendas = " +
                        idVendas);
                conn.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            } finally {
                try {
                    if (stmt != null) {
                        stmt.close();
                    }
                    if (conn != null) {
                        conn.close();
                    }
                } catch (SQLException e) {
                    System.out.println(e.getMessage());
                }
            }
        }
        return n;
	}

	public Integer alterar(Venda venda)
	{
            int n = 0;
            Connection conn = Conexao.getInstanciaUnica().criaConexao();

            if (conn != null) {
                PreparedStatement pstmt = null;
                try {
                    pstmt = conn.prepareStatement(
                            "UPDATE TB_VENDAS SET " +
                            "idEmpregado = ?, " +
                            "data = ?, " +
                            "descricao = ?, " +
                            "valor = ? " +
                            "WHERE idVendas = ?");

                    pstmt.setInt(1, venda.getIdEmpregado());
                    pstmt.setDate(2, (java.sql.Date)(venda.getData()));
                    pstmt.setString(3, venda.getDescricao());
                    pstmt.setFloat(4, venda.getValor());
                    pstmt.setInt(5, venda.getIdVendas());

                    n = pstmt.executeUpdate();
                    conn.close();

                } catch (SQLException ex) {
                    ex.printStackTrace();
                } finally {
                    try {
                        if (pstmt != null) {
                            pstmt.close();
                        }
                        if (conn != null) {
                            conn.close();
                        }
                    } catch (SQLException e) {
                        System.out.println(e.getMessage());
                    }
                }
            }
        return n;
	}
}